package com.belly.template;

import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.io.FileUtil;
import com.belly.template.enums.FileFilterRangeEnum;
import com.belly.template.enums.FileFilterRuleEnum;
import com.belly.template.model.FileFilterConfig;

import java.io.File;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Belly
 * @version 1.1.0
 *
 * 文件过滤器
 */
public class FileFilter {

    /**
     * 过滤多个文件
     * @param filePath
     * @param filterConfigList
     * @return
     */
    public static List<File> doFilter(String filePath, List<FileFilterConfig> filterConfigList){
        //获取路径下所有文件
        List<File> filePathList = FileUtil.loopFiles(filePath);
        return filePathList.stream()
                .filter(file -> doSingleFileFilter(filterConfigList, file))
                .collect(Collectors.toList());
    }

    /**
     * 单文件过滤处理
     * @param filterConfigList
     * @param file
     * @return
     */
    public static boolean doSingleFileFilter(List<FileFilterConfig> filterConfigList, File file){
        String fileName = file.getName();
        String fileContent = FileUtil.readUtf8String(file);
        boolean result = true;

        if (CollUtil.isEmpty(filterConfigList)){
            return true;
        }

        for (FileFilterConfig fileFilterConfig : filterConfigList) {
            String rule = fileFilterConfig.getRule();
            String range = fileFilterConfig.getRange();
            String value = fileFilterConfig.getValue();

            //处理文件过滤范围
            FileFilterRangeEnum fileFilterRangeEnum = FileFilterRangeEnum.getEnumByValue(range);
            //判断
            if (null == fileFilterRangeEnum){
                continue;
            }
            //过滤文件
            String content = fileName;
            switch (fileFilterRangeEnum) {
                case FILE_NAME:
                    content = fileName;
                    break;
                case FILE_CONTENT:
                    content = fileContent;
                    break;
                default:
            }

            //处理文件过滤规则
            FileFilterRuleEnum fileFilterRuleEnum = FileFilterRuleEnum.getEnumByValue(rule);
            //判断
            if (null == fileFilterRuleEnum){
                continue;
            }
            switch (fileFilterRuleEnum) {
                case REGEX:
                    result = content.matches(value);
                    break;
                case STARTS_WITH:
                    result = content.startsWith(value);
                    break;
                case EQUALS:
                    result = content.equals(value);
                    break;
                case ENDS_WITH:
                    result = content.endsWith(value);
                    break;
                case CONTAINS:
                    result = content.contains(value);
                    break;
                default:
            }
            if (!result){
                return false;
            }
        }
        return true;
    }
}
